<?php
 
include_once("./configure/configure.php");						//--> global var
include_once ("./admin/configure/admin.config.inc.php");			//-- admin var

include_once("impl/CouponImpl.php");
include_once("impl/CouponAttributeImpl.php");

include_once("impl/MerchantImpl.php");
include_once("impl/CategoryImpl.php");
include_once("constant/GlobalConstant.php");


$id		= 0;

if(isset($_POST['page'])){
	$page = $_POST['page'];
}else if(isset($_GET['page'])){
	$page = $_GET['page'];	
}else
	$page = 1;	// default featuered.

if(isset($_POST['catId'])){
	$catId = $_POST['catId'];
}else if(isset($_GET['catId'])){
	$catId = $_GET['catId'];	
}else
	$catId = 0;	// default featuered.

$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
$oCAttributeImpl= new CouponAttributeImpl(DB_TAG_COUPON);

$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);

$arrCoupons = array();

if($catId > 0){
	$catIdsStr 	= $oCategoryImpl->getLeafIdsStr($catId, true);
	$where 		= " WHERE status = 1 AND (isRecommend = 1 || isRecommend = 2) AND id IN (SELECT couponId FROM coupon_category WHERE categoryId in ($catIdsStr))";
}else{
	$where 	= " WHERE status = 1 AND (isRecommend = 1 || isRecommend = 2)";
}

$orderStr	= " ORDER BY r_sortValue DESC";


$oRecommendedCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 5);	
foreach ($oRecommendedCoupons as $oCoupon) {
	$arrCoupon = array();
	
	$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
	$oCoupon->setAttributes($oCouponAttribute);
	
	$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
	if(!is_object($oCategory))
		$oCategory = new CategoryDomain();
		
	$oCoupon->setCategory($oCategory);
	
	$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
	
	if($oCoupon->getCreativeType() == 1){
		$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
		
		if(is_object($oImage)){
			$oCoupon->setCouponImage($oImage);
				
			$arrCoupon['itUrl']		= $oCoupon->getCouponImage()->getImpressionTrackingUrl();
			$arrCoupon['baseItUrl'] = base64_encode($oCoupon->getCouponImage()->getImpressionTrackingUrl());			
		}else{
			$oCoupon->setCreativeType(2);
		}
	}

	if($oCoupon->getCreativeType() == 2){
		$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
		
		if(!is_object($oLogo)){
			$oLogo = new ImageDomain();
			
			$oLogo->setId(0);
			$oLogo->setExtName('gif');	
		}
		
		$oMerchant->setLogo($oLogo);
		
		$arrCoupon['logoId'] 	= $oMerchant->getLogo()->getId();
		$arrCoupon['logoEname'] = $oMerchant->getLogo()->getExtName();
	}			
	
	$oCoupon->setMerchant($oMerchant);
	
	
	
	
	$arrCoupon['id'] 		= $oCoupon->getId();
	$arrCoupon['name'] 		= $oCoupon->getName();
	
	$arrCoupon['tUrl'] 		= $oCoupon->getTrackingURL();
	$arrCoupon['code'] 		= $oCoupon->getCode();
	$arrCoupon['cType'] 	= $oCoupon->getCreativeType();
	$arrCoupon['typeStr'] 	= $oCoupon->getValidDateTypeStr();
	
	$arrCoupon['mname']		= $oCoupon->getMerchant()->getName();
	$arrCoupon['mNameId']	= $oCoupon->getMerchant()->getNameId();
	
	$arrCoupon['cid'] 		= $oCoupon->getCategory()->getId();
	$arrCoupon['cname'] 	= $oCoupon->getCategory()->getName();
	$arrCoupon['fname'] 	= $oCoupon->getCategory()->getFilteredName();
	
	$desc = $oCoupon->getDescript();
	if(empty($desc))
		$arrCoupon['desc'] 		= '';
	else
		$arrCoupon['desc'] 		= $oCoupon->getDescript();
	
	$ipixedl = $oCoupon->getImpressionpixel();
	if(empty($ipixedl))
		$arrCoupon['ipixedl'] 	= '';
	else
		$arrCoupon['ipixedl'] 	= $oCoupon->getImpressionpixel();
	
	if(empty($arrCoupon['tUrl'])){
		$arrCoupon['url']	= $oCoupon->getMerchant()->getDisplayURL();		
	}else{
		$arrCoupon['url']	= $arrCoupon['tUrl'];			
	}
	
	$arrCoupon['baseUrl']	= base64_encode($arrCoupon['url']);
	
	$arrCoupons[] = $arrCoupon;
}

//echo "=size " . count($arrCoupons);

$arrJsonData = array();
$arrJsonData['page'] 		= $page;
$arrJsonData['pageCount'] 	= $oCouponImpl->getPageCount();
$arrJsonData['coupons'] 	= $arrCoupons;

$jsonCoupons = json_encode($arrJsonData);
	
echo $jsonCoupons;
?>
